package cn.congine.wizarpos.mall.weChat.bussiness;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.congine.wizarpos.mall.entity.MyOnlineActivity;
import cn.congine.wizarpos.mall.entity.MyOnlineActivityDetail;
import cn.congine.wizarpos.mall.entity.MyRedEnvelopes;
import cn.congine.wizarpos.mall.entity.MyShake;
import cn.congine.wizarpos.mall.entity.SysInToken;
import cn.congine.wizarpos.mall.model.open.AccessTokenInput;
import cn.congine.wizarpos.mall.model.open.AccessTokenOutput;
import cn.congine.wizarpos.mall.model.open.AccessTokenOutput.Result;
import cn.congine.wizarpos.mall.model.open.CommonResultOutput;
import cn.congine.wizarpos.mall.model.open.DzxBindSendTicketInput;
import cn.congine.wizarpos.mall.model.open.DzxBindSendTicketOutput;
import cn.congine.wizarpos.mall.model.open.DzxWxHongbaoSendInput;
import cn.congine.wizarpos.mall.model.open.MemberReadRecordInput;
import cn.congine.wizarpos.mall.model.open.MemberReadRecordOutput;
import cn.congine.wizarpos.mall.model.open.MerchantDefInfoOutput;
import cn.congine.wizarpos.mall.model.open.MerchantDefQueryInput;
import cn.congine.wizarpos.mall.model.open.MerchantDefVo;
import cn.congine.wizarpos.mall.model.open.MrtWechartUserInfoUpdateInput;
import cn.congine.wizarpos.mall.model.open.MrtWechartUserInfoUpdateOutput;
import cn.congine.wizarpos.mall.model.open.SelfTicketAddInput;
import cn.congine.wizarpos.mall.model.open.SelfTicketAddOutput;
import cn.congine.wizarpos.mall.model.open.TicketDefUpdateInput;
import cn.congine.wizarpos.mall.model.open.TicketDefUpdateOutput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberBankCardBindInput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberBankCardBindOutput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberInfoQueryInput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberInfoQueryOutput;
import cn.congine.wizarpos.mall.model.open.WxCardBindInput;
import cn.congine.wizarpos.mall.model.open.WxCardBindOutput;
import cn.congine.wizarpos.mall.service.MyOnlineActivityDetailService;
import cn.congine.wizarpos.mall.service.MyRedEnvelopesService;
import cn.congine.wizarpos.mall.service.MyShakeService;
import cn.congine.wizarpos.mall.service.SysInTokenService;
import cn.congine.wizarpos.mall.service.impl.WPosHttpClient;
import cn.congine.wizarpos.mall.service.impl.WizarposOpenRestClient;
import cn.congine.wizarpos.mall.utils.CommonUtil;
import cn.congine.wizarpos.mall.utils.Function;
import cn.congine.wizarpos.mall.utils.SysConstants;
import cn.congine.wizarpos.mall.utils.WxSignUtil;
import cn.congine.wizarpos.mall.weChat.response.Article;
import cn.congine.wizarpos.mall.weChat.response.NewsMessage;
import cn.congine.wizarpos.mall.weChat.response.TextMessage;
import cn.congine.wizarpos.mall.weChat.util.MessageUtil;

import com.alibaba.fastjson.JSONObject;

/**
 * 处理微信推送消息
 * 
 * @author GuZhenJuan
 *
 */
@Component
public class WechartCoreService {
	private static Logger log = LoggerFactory
			.getLogger(WechartCoreService.class);

	@Autowired
	private WPosHttpClient wposHttpClient;
	@Autowired
	private WizarposOpenRestClient wizarposOpenRestClient;
	@Autowired
	private SysInTokenService sysInTokenService;
	@Autowired
	private MyRedEnvelopesService myRedEnvelopesService;
	@Autowired
	private MyShakeService myShakeService;
	@Autowired
	private MyOnlineActivityDetailService myOnlineActivityDetailService;

	/**
	 * 处理微信发来的请求
	 * 
	 * @param request
	 * @return
	 */
	public String processRequest(HttpServletRequest request,
			HttpServletResponse response) {
		String respMessage = null;
		try {
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			String fromUserName = requestMap.get("FromUserName");
			String toUserName = requestMap.get("ToUserName");
			String msgType = requestMap.get("MsgType");

			String respContent = "";
			if (MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgType)) {

				String eventType = requestMap.get("Event");
				ActionResponse actionResponse = null;
				if (MessageUtil.EVENT_TYPE_UNSUBSCRIBE.equals(eventType)) {
					// 用户取消关注
					actionResponse = unSubscribe(fromUserName, toUserName);

				} else if (MessageUtil.EVENT_TYPE_SUBSCRIBE.equals(eventType)
						|| MessageUtil.EVENT_TYPE_SCAN.equals(eventType)) {
					respContent = "谢谢您的关注，我们会努力做到更好。";
					// 用户订阅事件
					actionResponse = subscribe(fromUserName, toUserName);

					String eventKey = requestMap.get("EventKey");

					if (StringUtils.isEmpty(eventKey)) {
						String picUrl = "http://image.wizarpos.com/1003000100000092015043017295357654.png";
						String url = "http://mp.weixin.qq.com/s?__biz=MzA3OTE2OTY2OQ==&mid=215702317&idx=1&sn=7edb7065d64301980914e1d883e5bc3b#rd";
						String title = "谢谢您关注";
						String description = "您的关注是我们生存的动力";
						// 请求开放平台获取商户信息
						MerchantDefQueryInput merchantDefQueryInput = new MerchantDefQueryInput();
						merchantDefQueryInput.setPid(toUserName);
						MerchantDefInfoOutput merchantDefInfoOutput = (MerchantDefInfoOutput) wizarposOpenRestClient.post(merchantDefQueryInput, "/merchantdef/info/query", MerchantDefInfoOutput.class);
						if (merchantDefInfoOutput != null && "0".equals(merchantDefInfoOutput.getCode())) {
							MerchantDefInfoOutput.Result result = merchantDefInfoOutput.getResult();
							MerchantDefVo mechantDef = result.getMerchantDef();
							String welcomeContent = mechantDef.getWelcomeContent();
							JSONObject attachFields = (JSONObject) JSONObject.toJSON(result.getAttachFields());
							if ("TXT".equals(attachFields.getString("welcomeType"))) { // 文本类型直接返回
								if (!StringUtils.isEmpty(welcomeContent)) {
									respContent = welcomeContent;
								}								
								TextMessage textMessage = new TextMessage();
								textMessage.setToUserName(fromUserName);
								textMessage.setFromUserName(toUserName);
								textMessage.setCreateTime(new Date().getTime());
								textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
								textMessage.setFuncFlag(0);
								textMessage.setContent(respContent);
								respMessage = MessageUtil.textMessageToXml(textMessage);
								return respMessage;
							}
							if (!StringUtils.isEmpty(welcomeContent)) {
								url = welcomeContent;
							}
							JSONObject weixinImgText = JSONObject.parseObject(attachFields.getString("weixinImgText"));
							if (weixinImgText != null) {
								if (!StringUtils.isEmpty(weixinImgText.getString("title"))) {
									title = weixinImgText.getString("title");
								}
								if (!StringUtils.isEmpty(weixinImgText.getString("text"))) {
									description = weixinImgText.getString("text");
								}
								if (!StringUtils.isEmpty(weixinImgText.getString("imgUrl"))) {
									picUrl = weixinImgText.getString("imgUrl");
								}
							}
						}
						// 图文消息
						List<Article> articles = new ArrayList<Article>();
						Article article = new Article();
						article.setTitle(title);
						article.setDescription(description);
						article.setPicUrl(picUrl);
						article.setUrl(url);
						articles.add(article);

						NewsMessage newsMessage = new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage
								.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setFuncFlag(0);
						newsMessage.setArticleCount(1);
						newsMessage.setArticles(articles);
						respMessage = MessageUtil.newsMessageToXml(newsMessage);

						return respMessage;
					}

					// 桌台，发送点餐图文消息
					if (eventKey != null
							&& (eventKey.startsWith("tableId_") || eventKey
									.startsWith("qrscene_tableId_"))) {
						String[] data = eventKey.split("_");

						String mid = "";
						String tableId = "";
						if (data != null) {
							if (data.length == 3) {
								tableId = data[1];
								mid = data[2];

							} else if (data.length == 4) {
								tableId = data[2];
								mid = data[3];
							}

						}
						Function function = new Function();
						// 图文消息
						List<Article> articles = new ArrayList<Article>();
						Article article = new Article();
						article.setTitle("欢迎，请点餐");
						article.setDescription("欢迎，请点餐");
						article.setPicUrl("http://image.wizarpos.com/1003000100000092015043017295357654.png");
						article.setUrl(function.getBaseURL(request)
								+ "/dgd/tomenu?openId=" + fromUserName
								+ "&tableId=" + tableId + "&mid=" + mid);
						request.getSession().setAttribute("openId",
								fromUserName);
						request.getSession().setAttribute("mid", mid);

						// 请求开放平台查询会员卡信息
						WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
						input.setMid(mid);
						input.setOpenId(fromUserName);
						WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient
								.post(input, "/wechart/info/query",
										WeChartMemberInfoQueryOutput.class);
						if (output != null && "0".equals(output.getCode())) {
							if (output.getResult() != null) {
								request.getSession().setAttribute("mrtCardNo",
										output.getResult().getCardNo());
								request.getSession().setAttribute("mrtCardId",
										output.getResult().getId());
							} else {
								log.error("请求开放平台查询会员卡信息异常：result is null");
							}
						} else {
							log.error("请求开放平台查询会员卡信息异常：");
							if (output != null) {
								log.error(output.getMessage());
							}
						}

						articles.add(article);

						NewsMessage newsMessage = new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage
								.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setFuncFlag(0);
						newsMessage.setArticleCount(1);
						newsMessage.setArticles(articles);
						respMessage = MessageUtil.newsMessageToXml(newsMessage);
						return respMessage;
					}

					if (eventKey != null
							&& ("qrscene_0".equals(eventKey) || "0"
									.equals(eventKey))) {
						// 订阅并有二维码的参数值 0,用户银行卡绑定事件
						String ticketId = requestMap.get("Ticket");
						ticketId = URLEncoder.encode(ticketId, "utf-8");
						actionResponse = bankCardBind(fromUserName, ticketId);

					} else if (eventKey != null
							&& ("qrscene_1".equals(eventKey) || "1"
									.equals(eventKey))) {
						// 订阅并有二维码的参数值1,用户会员卡绑定事件
						String ticketId = requestMap.get("Ticket");
						ticketId = URLEncoder.encode(ticketId, "utf-8");
						actionResponse = cardBind(fromUserName, ticketId);
					} else if (eventKey != null
							&& ("qrscene_2".equals(eventKey) || "2"
									.equals(eventKey))) {
						// 订阅并有二维码的参数值2,东志信联盟商户二维码摇一摇关注送券事件
						String ticketId = requestMap.get("Ticket");
						ticketId = URLEncoder.encode(ticketId, "utf-8");
						actionResponse = dzxBindSendTicket(fromUserName,
								ticketId);
					} else if (eventKey != null
							&& ("qrscene_3".equals(eventKey) || "3"
									.equals(eventKey))) {
						// 订阅并有二维码的参数值3,发红包
						String ticketId = requestMap.get("Ticket");
						ticketId = URLEncoder.encode(ticketId, "utf-8");
						sendHongbao(fromUserName, ticketId);
					} else if (eventKey != null
							&& (eventKey.startsWith("qrscene_t_") || eventKey
									.startsWith("t_"))) {
						// 订阅并有二维码的参数值t_打头,用户扫描餐桌二维码
						String ticketId = requestMap.get("Ticket");
						ticketId = URLEncoder.encode(ticketId, "utf-8");
						String tableId = eventKey.substring(10);
						actionResponse = orderMenu(fromUserName, tableId);
					}
				} else if (MessageUtil.EVENT_TYPE_CLICK.equals(eventType)) {
					// 自定义菜单点击事件
					// String eventKey = requestMap.get("EventKey");
				} else if (MessageUtil.EVENT_TYPE_VIEW.equals(eventType)) {
					// String eventKey = requestMap.get("EventKey"); // TODO
					// 时候校验跳转地址
					// 记录用户访问
					//memberReadRecord(fromUserName, toUserName);
				} else if (MessageUtil.CARD_PASS_CHECK.equals(eventType)) {
					// 微信卡券审核通过
					String cardid = requestMap.get("CardId");
					String result = ticketPass(cardid, toUserName);
					if ("60038".equals(result)) {
						// 卡券审核事件推送太快的时候，门户的卡券定义还没有入库，无法更新卡券定义的审核状态，此时暂停先再请求
						for(int i=0; i < 3; i++) {
							Thread.sleep(5000);
							result = ticketPass(cardid, toUserName);
							if (!"60038".equals(result)) {
								break;
							}
						}
					}
				} else if (MessageUtil.CARD_NOT_PASS_CHECK.equals(eventType)) {
					// 卡券审核未通过处理
					String cardid = requestMap.get("CardId");
					ticketUnpass(cardid, toUserName);
				} else if (MessageUtil.USER_GET_CARD.equals(eventType)) {
					// 用户领券会员卡类型卡券事件
					String code = requestMap.get("UserCardCode");
					String wxCardId = requestMap.get("CardId");
					String isGiveByFriend = requestMap.get("IsGiveByFriend");
					String friendUser = requestMap.get("FriendUser");
					String oldTicketNo = requestMap.get("OldUserCardCode");
					String outerId = requestMap.get("OuterId");
					actionResponse = userGetCard(fromUserName, wxCardId, code, toUserName, isGiveByFriend, friendUser, oldTicketNo, outerId);
				} else if (MessageUtil.USER_CONSUME_CARD.equals(eventType)) {
					// 用户领券会员卡类型卡券事件
					// String code = requestMap.get("UserCardCode");
					// String cardId = requestMap.get("CardId");
					// userGetCard(fromUserName, code, toUserName);
					respContent = "您已成功使用了一张本商户优惠券，谢谢您的光顾，欢迎再次光临。";
				}
				if (actionResponse != null) {
					try {
						respContent = processResponse(response, actionResponse);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			} else if (MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType)) {
				respContent = "谢谢您发送消息";
			} else if (MessageUtil.REQ_MESSAGE_TYPE_IMAGE.equals(msgType)) {
				respContent = "谢谢您发送图片！";
			} else if (MessageUtil.REQ_MESSAGE_TYPE_LOCATION.equals(msgType)) {
				respContent = "谢谢您发送地理位置！";
			} else if (MessageUtil.REQ_MESSAGE_TYPE_LINK.equals(msgType)) {
				respContent = "谢谢您发送链接！";
			} else if (MessageUtil.REQ_MESSAGE_TYPE_VOICE.equals(msgType)) {
				respContent = "谢谢您发送音频！";
			}

			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setFuncFlag(0);
			textMessage.setContent(respContent);
			respMessage = MessageUtil.textMessageToXml(textMessage);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return respMessage;
	}

	/**
	 * 餐台扫码
	 * 
	 * @param fromUserName
	 * @param tableId
	 * @return
	 */
	private ActionResponse orderMenu(String openId, String tableId) {
		log.debug("--------------order menu scan------------");
		log.debug("openId is " + openId);
		log.debug("tableId is " + tableId);
		JSONObject message = new JSONObject();
		message.put("message", "谢谢您使用自助点餐，现在您可以从下方菜单选择您需要的服务");
		// TODO 加入模板消息推送，外部跳转链接到点餐页
		return new ActionResponse(0, "success", message);
	}

	/**
	 * 取消关注处理
	 * 
	 * @param parameter
	 * @return
	 */
	private ActionResponse unSubscribe(String openId, String pid) {
		log.debug("-----------unSubscribe ------------");
		log.debug("openId is " + openId);
		log.debug("pid is " + pid);

		MrtWechartUserInfoUpdateInput input = new MrtWechartUserInfoUpdateInput();
		input.setPid(pid);
		input.setOpenId(openId);
		input.setSubscribe("0");
		MrtWechartUserInfoUpdateOutput output = (MrtWechartUserInfoUpdateOutput) wizarposOpenRestClient
				.post(input, "/wxshop/userinfoupdate",
						MrtWechartUserInfoUpdateOutput.class);
		JSONObject message = new JSONObject();
		if (output == null) {
			message.put("message", SysConstants.SYSTEM_RRROR);
			return new ActionResponse(1, "error", message);
		} else if ("0".equals(output.getCode())) {
			message.put("message", "关注已取消");
			return new ActionResponse(0, "success", message);
		} else if ("60059".equals(output.getCode())) {
			message.put("message", SysConstants.NO_MERCHANT);
			return new ActionResponse(1, "error", message);
		} else {
			message.put("message", SysConstants.SYSTEM_RRROR);
			return new ActionResponse(1, "error", message);
		}
	}

	/**
	 * 用户关注处理
	 * 
	 * @param parameter
	 * @return
	 */
	private ActionResponse subscribe(String openId, String pid) {
		log.debug("--------------subscribe ------------");
		log.debug("openId is " + openId);
		log.debug("pid is " + pid);

		MrtWechartUserInfoUpdateInput input = new MrtWechartUserInfoUpdateInput();
		input.setPid(pid);
		input.setOpenId(openId);
		input.setSubscribe("1");
		MrtWechartUserInfoUpdateOutput output = (MrtWechartUserInfoUpdateOutput) wizarposOpenRestClient
				.post(input, "/dzx/userInfoUpdate",
						MrtWechartUserInfoUpdateOutput.class);
		JSONObject message = new JSONObject();
		if (output == null) {
			message.put("message", SysConstants.SYSTEM_RRROR);
			return new ActionResponse(1, "error", message);
		} else if ("0".equals(output.getCode())) {

//			if (output.getResult() != null
//					&& "1".equals(output.getResult().getFirstSubscribe())) {
//				// 首次关注 送券
//				try {
//					giftTicket4Subscribe(openId, output.getResult().getMid());
//				} catch (Exception e) {
//					log.debug("gift ticket for subscribe fail.");
//					e.printStackTrace();
//				}
//			}
			message.put("message", "谢谢您的关注，我们会努力做到更好。");
			return new ActionResponse(0, "success", message);
		} else if ("60059".equals(output.getCode())) {
			message.put("message", SysConstants.NO_MERCHANT);
			return new ActionResponse(1, "error", message);
		} else {
			message.put("message", SysConstants.SYSTEM_RRROR);
			return new ActionResponse(1, "error", message);
		}
	}

	/**
	 * 首次关注送券，送券的定义待营销规则二期完成，演示阶段券固定
	 * 
	 * @param openId
	 * @throws Exception
	 */
	private void giftTicket4Subscribe(String openId, String mid)
			throws Exception {

		// TODO 由mid查询发放的券
		String wxCardId = "pC_nujshJAfHjUkXEp3hONa-ocKY";

		String code = wposHttpClient.getTicketCode(mid);
		long timestamp = WxSignUtil.createTimestamp();

		// 请求微信card api的ticket
		String cardTicket = null;
		AccessTokenInput jsInput = new AccessTokenInput();
		jsInput.setMid(mid);
		CommonResultOutput jsOutput = (CommonResultOutput) wizarposOpenRestClient
				.post(jsInput, "/weixin/ticket/card", CommonResultOutput.class);
		if (jsOutput != null
				&& SysConstants.OPEN_SUCCESS.equals(jsOutput.getCode())) {
			Map<String, String> result = (Map<String, String>) jsOutput
					.getResult();
			cardTicket = result.get("ticket");
		} else {
			log.error("请求开放平台获取微信card api的ticket失败，首次关注送券失败");
		}
		String signature = WxSignUtil.extSign(cardTicket, wxCardId,
				String.valueOf(timestamp), code, "");

		JSONObject ext = new JSONObject();
		ext.put("code", code);
		ext.put("timestamp", timestamp);
		ext.put("signature", signature);

		JSONObject wxcard = new JSONObject();
		wxcard.put("card_id", wxCardId);
		wxcard.put("card_ext", ext.toString());

		JSONObject json = new JSONObject();
		json.put("touser", openId);
		json.put("msgtype", "wxcard");
		json.put("wxcard", wxcard);

		AccessTokenOutput output = (AccessTokenOutput) wizarposOpenRestClient
				.post(jsInput, "/weixin/accesstoken", AccessTokenOutput.class);
		String accessToken = null;
		if (output != null
				&& SysConstants.OPEN_SUCCESS.equals(output.getCode())) {
			Result apiTicket = output.getResult();
			if (apiTicket != null) {
				accessToken = apiTicket.getToken();
			} else {
				log.error("请求开放平台获取accesstoken失败，首次关注送券失败");
			}
		} else {
			log.error("请求开放平台获取accesstoken失败，首次关注送券失败");
		}

		String url = SysConstants.MESSAGE_CUSTOM_SEND_URL.replace(
				SysConstants.ACCESS_TOKEN, accessToken);

		wposHttpClient.post(url, json);
	}

	/**
	 * 银行卡绑定
	 * 
	 * @param parameter
	 * @return
	 */
	private ActionResponse bankCardBind(String openId, String ticketId) {
		// 返回消息
		JSONObject message = new JSONObject();
		WeChartMemberBankCardBindInput input = new WeChartMemberBankCardBindInput();
		input.setOpenId(openId);
		input.setTicketId(ticketId);
		input.setOperate("0");
		WeChartMemberBankCardBindOutput output = (WeChartMemberBankCardBindOutput) wizarposOpenRestClient
				.post(input, "/wechart/bankcard/update",
						WeChartMemberBankCardBindOutput.class);
		if (output == null) {
			message.put("message", "系统错误，会员卡绑定失败！");
			return new ActionResponse(1, "success", message);
		}
		message.put("message", output.getMessage());
		return new ActionResponse(0, "success", message);
	}

	/**
	 * 微信会员卡绑定实体卡
	 * 
	 * @param parameter
	 * @return
	 */
	private ActionResponse cardBind(String openId, String ticketId) {
		// 返回消息
		JSONObject message = new JSONObject();
		WxCardBindInput input = new WxCardBindInput();
		input.setOpenId(openId);
		input.setTicketId(ticketId);
		WxCardBindOutput output = (WxCardBindOutput) wizarposOpenRestClient
				.post(input, "/wxshop/wxCardBind", WxCardBindOutput.class);

		if (output == null || "-1".equals(output.getCode())) {// 系统错误，处理失败
			return new ActionResponse(1, "success", "系统错误，会员卡绑定失败！");
		} else if ("0".equals(output.getCode())) {// 操作成功
			message.put("message", "您已成功绑定实体会员卡与微信会员卡，更多优惠活动敬请期待！");
			return new ActionResponse(0, "success", message);
		} else if ("60041".equals(output.getCode())) {// 二维码已过期
			message.put("message", "本次小票的二维码已过期，下次刷卡消费后请及时扫描二维码。");
			return new ActionResponse(3, "error", message);
		} else if ("60064".equals(output.getCode())) {// 会员卡已绑定，无需再次绑定
			message.put("message", "你已经绑定了此会员卡，无需再次绑定");
			return new ActionResponse(1, "success", message);
		} else if ("60065".equals(output.getCode())) {// 会员卡已绑定其他微信会员，不能进行再次绑定
			message.put("message", "此会员卡已绑定其他微信会员，不能进行再次绑定！");
			return new ActionResponse(1, "success", message);
		} else if ("60066".equals(output.getCode())) {// 实体会员卡已绑定，不能进行再次绑定
			message.put("message", "您已经绑定过实体会员卡，不能进行再次绑定！");
			return new ActionResponse(1, "success", message);
		} else {// 60067，实体会员卡与微信会员卡绑定出错
			message.put("message", "实体会员卡与微信会员卡绑定出错，请联系商家！");
			return new ActionResponse(1, "fail", message);
		}

	}

	/**
	 * 记录用户访问
	 * 
	 * @param requestMap
	 * @return
	 */
	private void memberReadRecord(String openId, String pid) {

		log.debug("--------------member read record------------");
		log.debug("openId is " + openId);
		log.debug("pid is " + pid);
		MemberReadRecordInput input = new MemberReadRecordInput();
		input.setOpenId(openId);
		input.setPid(pid);
		MemberReadRecordOutput output = (MemberReadRecordOutput) wizarposOpenRestClient
				.post(input, "/wechart/memberReadRecord",
						MemberReadRecordOutput.class);

		if (output == null) {// 请求开放平台失败
			log.debug("请求开放平台失败。。。");
		} else if ("60019".equals(output.getCode())) {// 60019非法微信号
			log.debug(output.getMessage());
		} else {// 0 操作成功
			if (output.getResult() != null) {// 该pid下未找到商户
				log.debug(output.getResult());
			}
			log.debug(output.getMessage());
		}

	}

	// 处理结果
	private static String processResponse(HttpServletResponse response,
			ActionResponse actionResponse) throws IOException, ServiceException {
		response.setHeader("code", "" + actionResponse.getCode());
		if (null != actionResponse.getMessage()
				&& !"".equals(actionResponse.getMessage())) {
			response.setHeader("message",
					CommonUtil.base64Encoder(actionResponse.getMessage()));
		}

		String resJson = null;
		if (actionResponse.getData() != null) {
			resJson = actionResponse.getData().toString();
		}

		// 输出响应报文，json格式
		JSONObject message = JSONObject.parseObject(resJson);
		String resMsgs = message.getString("message");
		return resMsgs;
	}

	/**
	 * 微信卡券未审核通过
	 * 
	 * @param parameter
	 * @return
	 */
	private void ticketUnpass(String cardId, String gh) {
		TicketDefUpdateInput input = new TicketDefUpdateInput();
		input.setGhCode(gh);
		input.setWxCodeId(cardId);
		input.setWxAuditFlag("2");// 微信卡券审核标记 0待审核 1 审核通过 2 审核未通过
		TicketDefUpdateOutput output = (TicketDefUpdateOutput) wizarposOpenRestClient
				.post(input, "/wxshop/ticketDefUpdate",
						TicketDefUpdateOutput.class);
		if (output == null) {// 请求开放平台失败
			log.debug("请求开放平台失败。。。");
		} else if ("0".equals(output.getCode())) {// 0 操作成功
			log.debug(output.getMessage());
		} else {// 60059非法的商户公众号openid,60038未知的微信卡券ID,60008非法入参
			log.debug(output.getMessage());
		}
	}

	/**
	 * 微信卡券审核通过
	 * 
	 * @param parameter
	 * @return
	 */
	private String ticketPass(String cardId, String gh) {
		log.debug(cardId);
		TicketDefUpdateInput input = new TicketDefUpdateInput();
		input.setGhCode(gh);
		input.setWxCodeId(cardId);
		input.setWxAuditFlag("1");// 微信卡券审核标记 0待审核 1 审核通过 2 审核未通过
		TicketDefUpdateOutput output = (TicketDefUpdateOutput) wizarposOpenRestClient
				.post(input, "/wxshop/ticketDefUpdate",
						TicketDefUpdateOutput.class);
		if (output == null) {//请求开放平台失败
			log.debug("请求开放平台失败。。。");
		} else if ("0".equals(output.getCode())) {// 0 操作成功
			log.debug(output.getMessage());			
		} else {// 60059非法的商户公众号, 60038未知的微信卡券ID, 60008非法入参
			log.debug(output.getMessage());
			return output.getCode();
		}	
		return "";
	}

	/**
	 * 用户领取卡券
	 * 
	 * @param parameter
	 * @return
	 */
	private ActionResponse userGetCard(String openId, String wxCardId, String code, String gh, 
			String isGiceByFriend, String friendOpenId, String oldTicketNo, String outerId) {
		SelfTicketAddInput input = new SelfTicketAddInput();
		input.setPid(gh);
		input.setOpenId(openId);
		input.setWxCardId(wxCardId);		
		input.setTicketNo(code);	
		input.setWxAdded('1');
		input.setHbShared("0");
		input.setRemark("微信卡券领取推送");
		input.setIsGiceByFriend(isGiceByFriend);
		input.setFriendOpenId(friendOpenId);
		input.setOldTicketNo(oldTicketNo);
		input.setOuterId(outerId);
		SelfTicketAddOutput output = (SelfTicketAddOutput) wizarposOpenRestClient
				.post(input, "/wechart/selfTicketAdd",
						SelfTicketAddOutput.class);
		JSONObject message = new JSONObject();
		if (output == null) {// 请求开放平台失败
			log.error("请求开放平台异常");
			message.put("message", "");
			return new ActionResponse(1, "fail", message);
		} else if ("60059".equals(output.getCode())) {// 60059非法的公众号的gh号
			message.put("message", "商户公众号原始ID数据异常");
			return new ActionResponse(1, "fail", message);
		} else if ("60038".equals(output.getCode())) {// 60038未知的微信卡券ID
			message.put("message", output.getMessage());
			return new ActionResponse(1, "fail", message);
		} else if ("60068".equals(output.getCode())) {// 60068注册会员卡失败
			message.put("message", "快速创建会员卡失败");
			return new ActionResponse(1, "fail", message);
		} else if ("60069".equals(output.getCode())) {// 60069卡券已领取过
			message.put("message", "您已领取过该优惠券，请到微信卡包查看。");
			return new ActionResponse(1, "fail", message);
		} else if ("0".equals(output.getCode())) {// 0操作成功
			message.put("message", "恭喜您已成功领取了一张本商户的优惠券，请到微信卡包查看。");
			return new ActionResponse(0, "success", message);
		} else {// 其它code码 60008非法入参
			log.error(output.getMessage());
			message.put("message", "系统错误，请联系商家");
			return new ActionResponse(1, "fail", message);
		}
	}

	/**
	 * 
	 * 
	 * @param fromUserName
	 * @param ticketId
	 */
	private void sendHongbao(String fromUserName, String ticketId) {
		SysInToken token = sysInTokenService.getSysInTokenByTokenId(ticketId);
		if (token == null) {// 红包记录不存在
			return;
		}
		String id = token.getInfo(); // 红包定义id
		String activeId = token.getSubInfo();
		String mid = token.getMid();
		MyRedEnvelopes red = myRedEnvelopesService
				.getByMidAndMyShakeId(mid, id);
		if (red == null) { // 红包已经全部发完
			return;
		}
		Long amount = red.getAmount();
		MyOnlineActivity act = red.getMyOnlineActivity();
		String actName = act.getActivityName();
		String status = act.getStatus();
		if ("1".equals(status)) { // 1标示活动已经停止
			return;
		}

		// 请求开放平台发放红包
		DzxWxHongbaoSendInput input = new DzxWxHongbaoSendInput();
		input.setMid(mid);
		input.setOpenid(fromUserName);
		input.setActName(actName);
		input.setAmount(String.valueOf(amount));
		CommonResultOutput output = (CommonResultOutput) wizarposOpenRestClient
				.post(input, "/weixin/dzxSendWxHongbao",
						CommonResultOutput.class);

		if (output != null && "0".equals(output.getCode())) {
			// 红包发送成功
			MyShake myShake = myShakeService.get(id);
			MyOnlineActivityDetail myOnlineActivityDetail = new MyOnlineActivityDetail();
			myOnlineActivityDetail.setOnlineActivityId(activeId);
			myOnlineActivityDetail.setPersonOpenId(fromUserName);
			myOnlineActivityDetail.setReceiveTime(new Date());
			myOnlineActivityDetail.setTicketMid(myShake.getTicketMid());
			myOnlineActivityDetail.setTicketType(myShake.getTicketType());
			myOnlineActivityDetail.setTicketContent(myShake.getTicketContent());
			myOnlineActivityDetailService
					.saveMyOnlineActivityDetail(myOnlineActivityDetail);
			sysInTokenService.delete(token);
		}
	}

	/**
	 * 东志信联盟商户二维码摇一摇关注送券事件, 送券
	 * 
	 * @param fromUserName
	 * @param ticketId
	 * @return
	 */
	private ActionResponse dzxBindSendTicket(String fromUserName,
			String ticketId) {
		log.debug("--------------dzx openid binding and send ticket------------");

		// 请求开放平台完成 东志信联盟商户二维码摇一摇关注送券事件, 送券
		DzxBindSendTicketInput input = new DzxBindSendTicketInput();
		input.setTicketId(ticketId);
		input.setOpenId(fromUserName);
		DzxBindSendTicketOutput output = (DzxBindSendTicketOutput) wizarposOpenRestClient
				.post(input, "/dzx/bindSendTicket",
						DzxBindSendTicketOutput.class);
		JSONObject message = new JSONObject();
		if (output == null) {// 请求开放平台失败
			message.put("message", "系统错误，请联系商家");
			return new ActionResponse(1, "fail", message);
		} else if ("60041".equals(output.getCode())) {// 60041二维码已过期
			message.put("message", "本次小票的二维码已过期，下次刷卡消费后请及时扫描二维码。");
			return new ActionResponse(3, "error", message);
		} else if ("60079".equals(output.getCode())) {// 60079数据异常
			message.put("message", output.getMessage());
			return new ActionResponse(1, "fail", message);
		} else if ("60068".equals(output.getCode())) {// 60068注册会员卡失败
			message.put("message", "系统错误，会员卡绑定失败！");
			return new ActionResponse(1, "success", message);
		} else if ("0".equals(output.getCode())) {// 0操作成功
			message.put("message", "恭喜您摇一摇获得优惠券，在右下方【会员卡】栏查看优惠券详情。");
			return new ActionResponse(0, "success", message);
		} else {// 其它code码 -1 操作失败
			message.put("message", "系统错误，请联系商家");
			return new ActionResponse(1, "fail", message);
		}
	}

	/**
	 * 用户领取微信会员卡券
	 * 
	 * @param parameter
	 * @return
	 */
	/*
	 * @SuppressWarnings("unused") private ActionResponse
	 * userGetMemberCard(String openId, String cardId, String code, String gh) {
	 * SysWxChildMerchantToken mrt =
	 * sysWxChildMerchantTokenService.findByGhCode(gh);
	 * System.out.println("-----------userGetCard-----------");
	 * System.out.println(openId); String mid = mrt == null ? null :
	 * mrt.getMid(); // 判断当前的微信open_id是否已经开通微信会员卡 JSONObject json =
	 * wposHttpClient.getMerchantCardByOpenId(openId, mid); // 返回消息 JSONObject
	 * message = new JSONObject(); if (json != null && json.containsKey("code"))
	 * { int errcode = json.getIntValue("code"); if (errcode == NOCARD) { //
	 * 如果code=5,则说明没有对应的微信会员卡 // 如果没有微信会员卡，则通过open_id自动注册为微信会员卡 json =
	 * wposHttpClient.registerFreezeVipCard(openId, mid, code); errcode =
	 * json.getIntValue("code"); if (errcode == SUCCESS) {
	 * System.out.println(json.getString("id")); message.put("message",
	 * "您已成功领取本店的微信会员卡，请到微信卡包查看"); return new ActionResponse(0, "success",
	 * message); } else { message.put("message", "您领取本店微信会员卡异常，请联系商家"); return
	 * new ActionResponse(1, "fail", message); } } }
	 * 
	 * message.put("message", "系统异常，请联系商家"); return new ActionResponse(1,
	 * "fail", message);
	 * 
	 * }
	 */
}
